package top.hhydra.mimall.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import org.springframework.validation.BindingResult;
import top.hhydra.mimall.enums.ResponseEnum;

import java.util.Objects;

/**
 * @Description
 * @auther SaiyeEmon
 * @create 2020-02-09 4:26
 */
@Data
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class ResponseVo<T> {
    private Integer status;
    private String msg;
    private T data;

    public ResponseVo(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public ResponseVo(ResponseEnum responseEnum) {
        this.status = responseEnum.getCode();
        this.msg = responseEnum.getMsg();
    }

    public ResponseVo(ResponseEnum responseEnum, String msg) {
        this.status = responseEnum.getCode();
        this.msg = msg;
    }

    public ResponseVo(ResponseEnum responseEnum, T data) {
        this.status = responseEnum.getCode();
        this.msg = responseEnum.getMsg();
        this.data = data;
    }

    public static <T> ResponseVo<T> error(ResponseEnum responseEnum) {
        return new ResponseVo<>(responseEnum);
    }

    public static <T> ResponseVo<T> error(ResponseEnum responseEnum, String msg) {
        return new ResponseVo<>(responseEnum, msg);
    }

    public static <T> ResponseVo<T> error(ResponseEnum responseEnum, BindingResult bindingResult) {
        return new ResponseVo<>(responseEnum,
                Objects.requireNonNull(bindingResult.getFieldError()).getField()
                        + " " + bindingResult.getFieldError().getCode());
    }

    public static <T> ResponseVo<T> success() {
        return new ResponseVo<>(ResponseEnum.SUCCESS);
    }

    public static <T> ResponseVo<T> success(T data) {
        return new ResponseVo<>(ResponseEnum.SUCCESS, data);
    }

//    public static ResponseVo failByMsg(ResponseEnum responseEnum){
//        return new ResponseVo(responseEnum.getCode(),responseEnum.getMsg());
//    }
}
